package cn.remexs.apijson.upms.controller;

import apijson.RequestMethod;
import apijson.StringUtil;
import apijson.framework.APIJSONController;
import apijson.orm.Parser;

import java.net.URLDecoder;

import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**提供入口，转交给 APIJSON 的 Parser 来处理
 * @author Lemon
 */
@RestController
@RequestMapping("")
public class IndexController extends APIJSONController {

  @Override
  public Parser<Long> newParser(HttpSession session, RequestMethod method) {
    return super.newParser(session, method).setNeedVerify(false); //TODO 这里关闭校验，方便新手快速测试，实际线上项目建议开启
  }

	/**限制性GET，request和response都非明文，浏览器看不到，用于对安全性要求高的GET请求
	 * @param request 只用String，避免encode后未decode
	 * @param session
	 * @return
	 * @see {@link RequestMethod#GETS}
	 */
	@PostMapping("gets")
	@Override
	public String gets(@RequestBody String request, HttpSession session) {
		return super.gets(request, session);
	}

  /**获取
	 * @param request 只用String，避免encode后未decode
	 * @param session
	 * @return
	 * @see {@link RequestMethod#GET}
	 */
	@PostMapping(value = "get")
	@Override
	public String get(@RequestBody String request, HttpSession session) {
		return super.get(request, session);
	}

  /**获取
   * 只为兼容HTTP GET请求，推荐用HTTP POST，可删除
   * @param request 只用String，避免encode后未decode
   * @param session
   * @return
   * @see {@link RequestMethod#GET}
   */
  @RequestMapping("get/{request}")
  public String openGet(@PathVariable String request, HttpSession session) {
    try {
      request = URLDecoder.decode(request, StringUtil.UTF_8);
    } catch (Exception e) {
      // Parser会报错
    }
    return get(request, session);
  }
  	/**新增
	 * @param request 只用String，避免encode后未decode
	 * @param session
	 * @return
	 * @see {@link RequestMethod#POST}
	 */
	@PostMapping("post")
	@Override
	public String post(@RequestBody String request, HttpSession session) {
		return super.post(request, session);
	}
  /**修改
	 * @param request 只用String，避免encode后未decode
	 * @param session
	 * @return
	 * @see {@link RequestMethod#PUT}
	 */
	@PostMapping("put")
	@Override
	public String put(@RequestBody String request, HttpSession session) {
		return super.put(request, session);
	}
  /**删除
	 * @param request 只用String，避免encode后未decode
	 * @param session
	 * @return
	 * @see {@link RequestMethod#DELETE}
	 */
	@PostMapping("delete")
	@Override
	public String delete(@RequestBody String request, HttpSession session) {
		return super.delete(request, session);
	}
}
